package com.easy.controller;

import com.easy.bean.MemberType;
import com.easy.common.CommonResult;
import com.easy.service.IMemberTypeService;
import com.easy.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class MemberTypeController {

    @Autowired
    IMemberTypeService memberTypeS;

    @GetMapping("allmembertype")
    public CommonResult getAllMemberType(MemberType memberType, Page page){
        List<MemberType> list = memberTypeS.getAllMemberType(memberType,page);
        int count = memberTypeS.getMemberTypeCount(memberType);
        HashMap map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return CommonResult.success(map);
    }

    @PostMapping("membertype")
    public CommonResult addMemberType(@RequestBody MemberType memberType){
        memberTypeS.addMemberType(memberType);
        memberType = memberTypeS.getMemberTypeByID(memberType.getId());
        return CommonResult.success(memberType);
    }

    @DeleteMapping("membertype/{id}")
    public CommonResult delMemberType(@PathVariable int id){
        int res = memberTypeS.delMemberType(id);
        return CommonResult.success(res);
    }

    @PutMapping("membertype")
    public CommonResult editMemberType(@RequestBody MemberType memberType){
        memberTypeS.editMemberType(memberType);
        memberType = memberTypeS.getMemberTypeByID(memberType.getId());
        return CommonResult.success(memberType);
    }
}
